﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portfolio.IViews.Admin;
using Portfolio.BL.Categories;
using Portfolio.BL.Interfaces;
using Portfolio.Entities;
using Portfolio.Presenters.Interfaces;
using Portfolio.BL.Common;
using Portfolio.BL.Dictionary;

namespace Portfolio.Presenters.Admin
{
    /// <summary>
    /// Klasa presenter
    /// </summary>
    public class AdminCategoriesPresenter : IDefaultPresenter
    {
        private IAdminCategoriesView _view;        
        private CategoriesBL _categoriesBL;

        /// <summary>
        /// Konstruktor presenter.
        /// </summary>
        /// <param name="view">Interfejs widoku strony</param>
        public AdminCategoriesPresenter(IAdminCategoriesView view)
        {
            _view = view;            
            _categoriesBL = new CategoriesBL();
            this.Initialize();
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Inicjalizuje wszystkie zdarzenia na stronie
        /// </summary>
        public void Initialize()
        {
            this._view.LoadCategoriesSortBySearch += new EventHandler<EventArgs>(_viewLoadCategoriesSortBySearch);
            this._view.DeleteCategory += new EventHandler<EventArgs>(_viewDeleteCategory);
            this._view.LoadFieldsSearch += new EventHandler<EventArgs>(_viewLoadFieldsSearch);
            this._view.SetMaxSeqCategory += new EventHandler<EventArgs>(_viewSetMaxSeqCategory);
            this._view.SetUpSeq += new EventHandler<EventArgs>(_viewSetUpSeq);
            this._view.SetDownSeq += new EventHandler<EventArgs>(_viewSetDownSeq);
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie wykonuje sortowanie kategorii przez podane parametry
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadCategoriesSortBySearch(object sender, EventArgs e)
        {            
            _categoriesBL.ListArticleCategoriesBySearch(_view.SearchTitle, _view.SearchCategories, _view.SearchStatus, _view.SearchActive, _view.SortExpression, _view.SortDirection);
            _view.ArticleCategoriesDataBind = _categoriesBL.ListGridArticleCategories;            
            _view.PublishException = _categoriesBL.InformationException;            
        }

        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie zlicza wszystkie kategorie w konkretnej rodzinie, służy do użycia strzałek w gridzie
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetMaxSeqCategory(object sender, EventArgs e)
        {
            _view.MaxSeqCategory = _categoriesBL.ReturnMaxSeqArticleCategoryById(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishException = _categoriesBL.InformationException;
        }

               
        /// <summary>
        /// Metoda użytkownika
        /// Zdarzenie wczytuje wszystkie pola do wyszukiwarki kategorii
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewLoadFieldsSearch(object sender, EventArgs e)
        {
            CommonDictionary _commonDictionary = CommonDictionary.Instance;

            _view.CategoriesDataSource = new IViews.Common.DataSourceContainer("Name", "ArticleCategoryID", _categoriesBL.ReturnArticleCategoriesDDL(), null);
            _view.StatusDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _commonDictionary.GetStatusDictionary(), null);
            _view.ActiveDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _commonDictionary.GetActiveDictionary(), null);
            _view.PageSizeDataSource = new IViews.Common.DataSourceContainer("Value", "Key", _commonDictionary.GetPageSizeDictionary(), 1);

            _view.PublishException = _categoriesBL.InformationException;            
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie usuwa kategorię
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewDeleteCategory(object sender, EventArgs e)
        {
            _categoriesBL.DeleteArticleCategory(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishInformationSuccess = _categoriesBL.InformationSuccess;
            _view.PublishInformationNotice = _categoriesBL.InformationNotice;
            _view.PublishException = _categoriesBL.InformationException;           
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie zmienia kolejność w gridzie o jeden wiersz w górę w podkategorii
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetUpSeq(object sender, EventArgs e)
        {
            _categoriesBL.UpSeqArticleCategory(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishInformationSuccess = _categoriesBL.InformationSuccess;
            _view.PublishException = _categoriesBL.InformationException;
        }

        /// <summary>
        /// Metoda użytkownika.
        /// Zdarzenie zmienia kolejność w girdzie o jeden wiersz w dół
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _viewSetDownSeq(object sender, EventArgs e)
        {
            _categoriesBL.DownSeqArticleCategory(_view.ArticleCategory.ArticleCategoryID);
            _view.PublishInformationSuccess = _categoriesBL.InformationSuccess;
            _view.PublishException = _categoriesBL.InformationException;            
        }
    }
}
